package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 注:这里的方法实现只是将好友关系保存到本地数据库中
 */
@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {
    /**
     * 添加好友,本质就是将这种好友关系保存到mongodb中
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Long userId, Long friendId) {
        //1.判断userIdhe friendId是否为空
        if (ObjectUtil.isEmpty(userId) && ObjectUtil.isEmpty(friendId)) {
            return null;
        }
        //2.判断该两人是否已经是好友关系
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count > 0) {
            return null;
        }
        //3.保存好友关系
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        users.setUserId(userId);
        users.setFriendId(friendId);
        mongoTemplate.save(users);
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        users.setFriendId(userId);
        users.setUserId(friendId);
        mongoTemplate.save(users);
        //将这条数据的id返回
        return users.getId().toHexString();
    }

    /**
     * 删除好友,本质就是将这种好友关系从mongodb中删除
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
      /*  //1.判断userIdhe friendId是否为空
        if(ObjectUtil.isEmpty(userId)&&ObjectUtil.isEmpty(friendId)){
            return false;
        }
        //2.判断该两人是否是好友关系
        Query query =  Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if(count>0){
            return false;
        }*/
        //3.删除好友关系
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long deletedCount1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();
        Query query2 = Query.query(Criteria.where("friendId").is(userId).and("userId").is(friendId));
        long deletedCount2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();
        if (deletedCount1 > 0 && deletedCount2 > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询好友列表全表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        //根据userId查询所有其对应的users
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Users> users = mongoTemplate.find(query, Users.class);
        return users;
    }

    /**
     * 分页查询好友列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> users = mongoTemplate.find(query, Users.class);
        pageInfo.setRecords(users);
        return pageInfo;
    }
}
